package stage2.module2.Q1;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtils {

    public static DataSource dataSource;

    static {
        try {
            Properties p = new Properties();

            InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");

            p.load(inputStream);

            dataSource = DruidDataSourceFactory.createDataSource(p);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
